//
//  FindPasswordViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/5.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "FindPasswordViewController.h"

@interface FindPasswordViewController ()
@property (nonatomic, strong) UITextField *phoneNumTextF;  // 手机号
@property (nonatomic, strong) UITextField *verificationCodeTextF;  // 验证码
@property (nonatomic, strong) UIButton *verificationBtn;  // 验证码
@property (nonatomic, strong) UITextField *passwordTextF;  // 密码
@property (nonatomic, strong) UITextField *ensurePasswordTextF;  // 确认密码
@end

@implementation FindPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)setupUI
{
    UIImageView *bgImg = [[UIImageView alloc] init];
    bgImg.image = [UIImage imageNamed:@"loginBGImg"];
    bgImg.userInteractionEnabled = YES;
    [self.view addSubview:bgImg];
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setHitEdgeInsets:UIEdgeInsetsMake(-15, -15, -15, -15)];
    [bgImg addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(59);
        make.left.offset(26);
        make.width.height.mas_equalTo(16);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"用户登录";
    titleLabel.font = JSS_MEDIUM_FONT(34);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    [bgImg addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(107);
        make.left.offset(35);
        make.right.offset(-35);
        make.height.mas_equalTo(48);
    }];
    
    self.phoneNumTextF = [InsetsTextField new];
//    self.phoneNumTextF.placeholder = @"手机号";
    NSMutableAttributedString *phoneplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.phoneNumTextF.attributedPlaceholder = phoneplaceholderString;
    self.phoneNumTextF.font = JSS_MEDIUM_FONT_7;
//    self.phoneNumTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.phoneNumTextF.layer.masksToBounds = YES;
    self.phoneNumTextF.layer.cornerRadius = 14;
    self.phoneNumTextF.backgroundColor = UIColor.whiteColor;
    [bgImg addSubview:self.phoneNumTextF];
    [self.phoneNumTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(titleLabel.mas_bottom).offset(19);
        make.right.offset(-147);
        make.height.mas_equalTo(56);
    }];
    
    self.verificationCodeTextF = [InsetsTextField new];
    NSMutableAttributedString *verificationCodeplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"验证码" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.verificationCodeTextF.attributedPlaceholder = verificationCodeplaceholderString;
//    self.verificationCodeTextF.placeholder = @"验证码";
    self.verificationCodeTextF.font = JSS_MEDIUM_FONT_7;
//    self.verificationCodeTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.verificationCodeTextF.layer.masksToBounds = YES;
    self.verificationCodeTextF.layer.cornerRadius = 14;
    self.verificationCodeTextF.backgroundColor = UIColor.whiteColor;
    [bgImg addSubview:self.verificationCodeTextF];
    [self.verificationCodeTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.phoneNumTextF.mas_bottom).offset(10);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    
    self.verificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.verificationBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [self.verificationBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    self.verificationBtn.layer.masksToBounds = YES;
    self.verificationBtn.layer.cornerRadius = 14;
    self.verificationBtn.backgroundColor = RGB_VALUE(0xFFB100);
    [self.verificationBtn addTarget:self action:@selector(verificationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:self.verificationBtn];
    [self.verificationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneNumTextF.mas_right).offset(6);
        make.centerY.equalTo(self.phoneNumTextF);
        make.right.offset(-28);
        make.height.equalTo(self.phoneNumTextF);
    }];
    
    self.passwordTextF = [InsetsTextField new];
    NSMutableAttributedString *passwordplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"设置新密码" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.passwordTextF.attributedPlaceholder = passwordplaceholderString;
//    self.passwordTextF.placeholder = @"设置新密码";
    self.passwordTextF.font = JSS_MEDIUM_FONT_7;
//    self.passwordTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.passwordTextF.layer.masksToBounds = YES;
    self.passwordTextF.layer.cornerRadius = 14;
    self.passwordTextF.backgroundColor = UIColor.whiteColor;
    self.passwordTextF.secureTextEntry = YES;
    [bgImg addSubview:self.passwordTextF];
    [self.passwordTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.verificationCodeTextF.mas_bottom).offset(10);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    
    self.ensurePasswordTextF = [InsetsTextField new];
    NSMutableAttributedString *ensurepasswordplaceholderString = [[NSMutableAttributedString alloc] initWithString:@"确认新密码" attributes:@{NSForegroundColorAttributeName : RGB_VALUE(0xA5A5A5)}];
    self.ensurePasswordTextF.attributedPlaceholder = ensurepasswordplaceholderString;
//    self.ensurePasswordTextF.placeholder = @"确认新密码";
    self.ensurePasswordTextF.font = JSS_MEDIUM_FONT_7;
//    self.ensurePasswordTextF.textColor = RGB_VALUE(0xA5A5A5);
    self.ensurePasswordTextF.layer.masksToBounds = YES;
    self.ensurePasswordTextF.layer.cornerRadius = 14;
    self.ensurePasswordTextF.backgroundColor = UIColor.whiteColor;
    self.ensurePasswordTextF.secureTextEntry = YES;
    [bgImg addSubview:self.ensurePasswordTextF];
    [self.ensurePasswordTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.equalTo(self.passwordTextF.mas_bottom).offset(10);
        make.right.offset(-28);
        make.height.mas_equalTo(56);
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"确定" forState:UIControlStateNormal];
    [loginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    loginBtn.backgroundColor = RGB_VALUE(0xDC0000);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = 14;
    [loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImg addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ensurePasswordTextF.mas_bottom).offset(30);
        make.right.offset(-28);
        make.left.offset(28);
        make.height.mas_equalTo(56);
    }];
}

- (void)backBtnClick
{
//    [self pop];
    [self.navigationController popViewControllerAnimated:YES];
}

// 获取验证码
- (void)verificationBtnClick:(UIButton *)sender
{
    
    NSTimeInterval seconds =120;
    NSDate *endTime = [NSDate dateWithTimeIntervalSinceNow:seconds];// 最后期限
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_source_t _sometimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);
    dispatch_source_set_timer(_sometimer, dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);// 每秒执行一次
    dispatch_source_set_event_handler(_sometimer, ^{
        int interval = [endTime timeIntervalSinceNow];
        if(interval >0) {// 更新倒计时
            NSString *timeStr = [NSString stringWithFormat:@"%d秒内有效", interval];
            dispatch_async(dispatch_get_main_queue(), ^{
                [sender setTitle:timeStr forState:UIControlStateNormal];                     sender.userInteractionEnabled =NO;
                [sender setTitleColor:RGB_VALUE(0xA5A5A5) forState:UIControlStateNormal];
            });
        }else{
            // 倒计时结束，关闭
            
            dispatch_source_cancel(_sometimer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [sender setTitle:@"获取验证码"forState:UIControlStateNormal];
                [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];                sender.userInteractionEnabled =YES;
            });
        }
    });
    dispatch_resume(_sometimer);
}

// 确认
- (void)loginBtnClick:(UIButton *)sender
{
    
}
@end
